package wang.wangby.autoconfigure.web;

import java.util.Properties;

import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import wang.wangby.utils.converter.JsonUtil;
import wang.wangby.utils.template.TemplateUtil;
import wang.wangby.utils.template.TrimAllDirective;

@Configuration
@EnableConfigurationProperties({ VelocityProperties.class })
public class WebAutoConfiguration {
	
	VelocityProperties velocityProperties;
	
	public WebAutoConfiguration(VelocityProperties velocityProperties) {
		this.velocityProperties=velocityProperties;
	}
	
	@Bean
	public TemplateUtil templateUtil() {
		VelocityEngine velocityEngine = new VelocityEngine();
		Properties p = new Properties();
		p.setProperty("file.resource.loader.class", ClasspathResourceLoader.class.getName());
		p.setProperty("userdirective", TrimAllDirective.class.getName());
		p.setProperty("input.encoding", velocityProperties.getEncoding());
		p.setProperty("output.encoding", velocityProperties.getEncoding());
		p.setProperty("runtime.log", velocityProperties.getLog());
		velocityEngine.init(p);
		return new TemplateUtil(velocityEngine,velocityProperties.getRoot());
	}
	
	@Bean
	public JsonUtil jsonUtil() {
		return new JsonUtil();
	}
}
